package com.elvende.web.client;

import com.elvende.web.client.event.LearningDrillEvent;
import com.elvende.web.client.event.LearningDrillEventHandler;
import com.elvende.web.client.event.ViewHeritageEvent;
import com.elvende.web.client.event.ViewHeritageEventHandler;
import com.elvende.web.client.event.ViewLifeEvent;
import com.elvende.web.client.event.ViewLifeEventHandler;
import com.elvende.web.client.event.WelcomeEvent;
import com.elvende.web.client.event.WelcomeEventHandler;
import com.elvende.web.client.presenter.HeritagePresenter;
import com.elvende.web.client.presenter.LifePresenter;
import com.elvende.web.client.presenter.Presenter;
import com.elvende.web.client.presenter.WelcomePresenter;
import com.elvende.web.client.presenter.life.LearningDrillPresenter;
import com.elvende.web.client.presenter.life.LearningPresenter;
import com.elvende.web.client.service.GreetingService;
import com.elvende.web.client.service.GreetingServiceAsync;
import com.elvende.web.client.service.LifeService;
import com.elvende.web.client.service.LifeServiceAsync;
import com.elvende.web.client.service.life.LearningService;
import com.elvende.web.client.service.life.LearningServiceAsync;
import com.elvende.web.client.view.HeritageView;
import com.elvende.web.client.view.LifeView;
import com.elvende.web.client.view.WelcomeView;
import com.elvende.web.client.view.life.LearningDrillView;
import com.elvende.web.client.view.life.LearningView;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.HasWidgets;

public class AppController implements ValueChangeHandler<String>{
	
	private final HandlerManager eventBus;
	private HasWidgets container;
	
	GreetingServiceAsync greetRpcService = GWT.create(GreetingService.class);
	LifeServiceAsync lifeRpcService = GWT.create(LifeService.class);
	LearningServiceAsync learningRpcService = GWT.create(LearningService.class);
	
	public AppController(HandlerManager eventBus){
		this.eventBus = eventBus;
		bind();			
	}
	
	private void bind() {
		History.addValueChangeHandler(this);
		 eventBus.addHandler(ViewHeritageEvent.TYPE,
			      new ViewHeritageEventHandler() {					
					public void onViewHeritage(ViewHeritageEvent event) {
						doViewHeritage();
					}
		  });	
		 eventBus.addHandler(ViewLifeEvent.TYPE,
			      new ViewLifeEventHandler() {					
					public void onViewLife(ViewLifeEvent event) {
						doViewLife();
					}
		  });	
		 eventBus.addHandler(WelcomeEvent.TYPE,
			      new WelcomeEventHandler() {					
					public void onWelcome(WelcomeEvent event) {
						doWelcome();						
					}
		  });		 
		 eventBus.addHandler(LearningDrillEvent.TYPE,
			      new LearningDrillEventHandler() {
					public void onAction(LearningDrillEvent event) {
						doDrill();						
					}			 
		  });		 
	}
	
	public void onValueChange(ValueChangeEvent<String> event) {	
	    String token = event.getValue();
	    
	    if (token != null) {
	      Presenter presenter = null;
	 
	      if (token.equals("welcome")) {
	        presenter = new WelcomePresenter(greetRpcService, eventBus, new WelcomeView());	        
	      }else if (token.equals("heritage")) {
	          presenter = new HeritagePresenter(greetRpcService, eventBus, new HeritageView());
	      }else  if (token.equals("life")) {
	          presenter = new LifePresenter(lifeRpcService, eventBus, new LifeView());
	      }else  if (token.equals("learning")) {
	          presenter = new LearningPresenter(learningRpcService, eventBus, new LearningView());
	      }else  if (token.equals("learningDrill")) {
	          presenter = new LearningDrillPresenter(learningRpcService, eventBus, new LearningDrillView());
	      }	      
	      if (presenter != null)
	    	  presenter.go(container);	      
	    }
	}
	
	private void doViewHeritage(){
		History.newItem("heritage");
	}
	
	private void doViewLife(){
		History.newItem("life");
	}
	
	private void doDrill(){
		History.newItem("learningDrill");
	}
	
	private void doWelcome(){
		History.newItem("welcome");
	}
	
	  public void go(final HasWidgets container) {
		    this.container = container;
		    
		    if ("".equals(History.getToken())) {
		      History.newItem("learning");
		    }
		    else {
		      History.fireCurrentHistoryState();
		    }
	}
}